package nancal.mp.feign

import nbcp.base.comm.ListResult
import nbcp.base.model.HttpInvoke
import nbcp.base.utils.WebUtil
import nbcp.http.HttpClient
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RequestHeader

@HttpClient("registry")
interface RegistryClient {

    data class CatalogResult(
        var repositories: List<String> = listOf()
    )

    @GetMapping("/v2/_catalog")
    fun getCatalog(@RequestHeader("Authorization") authorization: String): CatalogResult


    fun getCatalog(userName: String, password: String): List<String> {
        var list = getCatalog(WebUtil.getBasicAuthorization(userName, password));
        return list.repositories;
    }


    class TagResult {
        var name: String = ""
        var tags: List<String> = listOf()
    }

    @GetMapping("/v2/{name}/tags/list")
    fun getTags(@RequestHeader("Authorization") authorization: String, @PathVariable("name") name: String): TagResult

    fun getTags(userName: String, password: String, name: String): List<String> {
        var result = getTags(WebUtil.getBasicAuthorization(userName, password), name);
        return result.tags.sortedByDescending { it }
    }
}